home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swags_z.zip / STRINGS.SWG / 0089_Pos() in asm.pas < prev    next >
Pascal/Delphi Source File  |  1994-08-24  |  3KB  |  58 lines

  1.  
  2. var
  3.   s1, s2: string;
  4.   position: byte;
  5.  
  6. function StrPos( var str1, str2: string ): byte; assembler;
  7.   { returns position of the first occurrence of str1 in str2 }
  8.   { return value in AL }
  9.   { str1 - string to search for }
  10.   { str2 - string to search in  }
  11. asm
  12.         CLD              { string operations forward                 }
  13.         LES   DI,Str2    { load in ES:DI pointer to str2             }
  14.         XOR   CH,CH      { clear CH                                  }
  15.         MOV   CL,[DI]    { length str2 --> CL                        }
  16.         AND   CL,CL      { length str2 = 0?                          }
  17.         JZ    @Negatief  { length str2 = 0, nothing to search in     }
  18.         MOV   BH,CL      { length str2 --> BH                        }
  19.         INC   DI         { make DI point to the 1st char of str2     }
  20.         LDS   SI,Str1    { load in DS:SI pointer to str1             }
  21.         LODSB            { load in AL length str1                    }
  22.         AND   AL,AL      { length str1 = 0?                          }
  23.         JZ    @Negatief  { length str1 = 0, nothing to search for    }
  24.         DEC   AL         { 1st char need not be compared again       }
  25.         SUB   CL,AL      { length str2 - length str1                 }
  26.         JBE   @Negatief  { length str2 < length str1                 }
  27.         MOV   AH,AL      { length str1 --> AH                        }
  28.         LODSB            { load in AL 1st character of str1          }
  29. @Start:
  30.   REPNE SCASB            { scan for next occurrence 1st char in str2 }
  31.         JNE   @Negatief  { no success                                }
  32.         MOV   DX,SI      { pointer to 2nd char in str1 --> DX        }
  33.         MOV   BL,CL      { number of chars in str2 to go --> BL      }
  34.         MOV   CL,AH      { length str1 --> CL                        }
  35.    REPE CMPSB            { compare until characters don't match      }
  36.         JE    @Positief  { full match                                }
  37.         SUB   SI,DX      { current SI - prev. SI = # of chars moved  }
  38.         SUB   DI,SI      { current DI - # of chars moved = prev. DI  }
  39.         MOV   SI,DX      { restore pointer to 2nd char in str1       }
  40.         MOV   CL,BL      { number of chars in str2 to go --> BL      }
  41.         JMP   @Start     { scan for next occurrence 1st char in str2 }
  42. @Negatief:
  43.         XOR   AX,AX      { str1 is not in str2, result 0             }
  44.         JMP   @Exit
  45. @Positief:
  46.         ADD   BL,AH      { number of chars in str2 left              }
  47.         MOV   AL,BH      { length str2 --> AX                        }
  48.         SUB   AL,BL      { start position of str1 in str2            }
  49. @Exit:                   { we are finished. }
  50. end  { StrPos };
  51.  
  52. begin
  53.   s1 := ParamStr( 1 );
  54.   s2 := ParamStr( 2 );
  55.   writeln( StrPos( s1, s2 ) );
  56. end.
  57.  
  58.